<template>
  <div>
    <el-dialog title="所属菜单" :visible.sync="dialogRoleRelMenuFormVisible"   width="50%">

      <el-tree
        :data="data"
        show-checkbox
        node-key="id"
        default-expand-all
        ref="tree"
        highlight-current
        :default-checked-keys="efaultCheckedKeys"
        :props="defaultProps">
      </el-tree>

       <el-button @click="getCheckedKeys">保存</el-button>


    </el-dialog>

    <SysUserSelect ref="sysUserSelectRef" @on-result-change="_userSelected"></SysUserSelect>
  </div>


</template>

<script>
 import  SysUserSelect from '../common/SysUserSelect'
    export default {
      components:{
        SysUserSelect
      },
      mounted(){
        this._listParent();
      },

        name: "SysRoleEdit",
      data(){
          return{



            roleId:'',


            data: [],
            efaultCheckedKeys: [],
            defaultProps: {
              children: 'children',
              label: 'label'
            },
            defaultExpand:true,
            parentName: '',



            dialogRoleRelMenuFormVisible: false,

            formLabelWidth: '120px'
          }
      },
      methods:{

          _open(obj){
            this.roleId=obj.id;
            this.dialogRoleRelMenuFormVisible=true;
            this._listParent();
          },

        _userSelected(user){
         let data={roleId:this.roleId,userId:user.id};
          this.$http.post('/sys_role_rel_user/add',data).then(res=>{
            this._page();
            this.$message({
              type: 'success',
              message: '添加成功!'
            });


          }).catch(err=>{
            this.$message({
              type: 'info',
              message: '添加失败'
            });
          })
        },
        handleSizeChange(val) {
          this.pageSize=val;
          this._page();
        },
        handleCurrentChange(val) {
          this.currentPage=val;
          this._page();
        },


        getCheckedKeys() {
          let data = this.$refs.tree.getCheckedKeys();
          console.log('----------',data);
          this.$http.post('/api/base/sys_role/add_role_resource?roleId='+this.roleId+'&resourceIds='+data).then(res=>{
            this.$message({
              type: 'success',
              message: '保存成功!'
            });
            this.dialogRoleRelMenuFormVisible=false;
           this._listParent();
          }).catch(err=>{
          });
        },

        _pNameChange(data){
            this.form.pid=data;
        },

        _listParent(){
          this.$http.post('/api/base/sys_resource/list_menu_by_role',{roleId:this.roleId}).then(res=>{
                this.data=res.data.data;
            this.efaultCheckedKeys=[];
            for(let i=0;i<this.data.length;i++){
              if (this.data[i].checked==1){
                      if (this.data[i].pid==0){
                        let childrens=this.data[i].children;
                        for (let j=0;j<childrens.length;j++) {
                             if (childrens[j].checked==1){
                               this.efaultCheckedKeys.push(childrens[j].id);
                             }
                        }
                      }
                     this.efaultCheckedKeys.push(this.data[i].id);
              }else{
                if (this.data[i].pid==0){
                  let childrens=this.data[i].children;
                  for (let j=0;j<childrens.length;j++) {
                    if (childrens[j].checked==1){
                      this.efaultCheckedKeys.push(childrens[j].id);
                    }
                  }
                }
              }
            }
            console.log('========this.efaultCheckedKeys',this.efaultCheckedKeys)
          }).catch(err=>{

          });
        },

      },
    }
</script>

<style scoped>

</style>
